package com.example.design.factory;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ReflectUtil;
import com.example.design.constant.PayStrategyEnum;
import com.example.design.service.IPayment;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author Hong Qiang
 * @Date 2023/3/27 14:13
 * @Version 1.0
 */
@Component
public class PaymentFactory {

    public static IPayment getPayStrategy(String type) {
        // 1.通过枚举中的type获取对应的value
        String value = EnumUtil.getFieldBy(PayStrategyEnum::getValue, PayStrategyEnum::getType, type);
        // spring.factotries.使用反射机制创建对应的策略类
        IPayment payment = ReflectUtil.newInstance(value);
        return payment;
    }
}
